package com.fanbai.score.common.base;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;


import com.fanbai.score.R;
import com.fanbai.score.common.Manager.ActivityManager;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.view.loadding.CustomDialog;

import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity {

    /**
     * 上下文
     */
    protected BaseActivity ctx;
    public Toolbar toolbar;
    private CustomDialog dialog;//进度条


    protected final String TAG = getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        super.onCreate(savedInstanceState);
        setContentView(getLayoutID());
        ButterKnife.bind(this);
        ActivityManager.getInstance().addActivity(this);
        dealCommonPart();
        initView();
        initData();
        initListener();
    }

    /**
     * 处理公共逻辑
     */
    private void dealCommonPart() {
        this.ctx = this;
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if(toolbar != null){
            toolbar.setTitle(getTitleName());
            int navigationIcon = getTitleLeftName();
            if(navigationIcon > 0){
                toolbar.setNavigationIcon(navigationIcon);//设置导航栏图标

            }
            if(getTitleBarColor() > -1){
                toolbar.setBackgroundResource(getTitleBarColor());
            }
            if(getTitleTextColor() > -1){
                toolbar.setTitleTextColor(getTitleTextColor());
            }
            setSupportActionBar(toolbar);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickTitleLeft();
                }
            });
        }

    }

    /**
     * 获取布局id
     *
     * @return 布局id
     */
    protected abstract int getLayoutID();

    /**
     * 获取标题栏中间文字
     *
     * @return
     */
    public abstract String getTitleName();

    /**
     * 获取标题栏的背景颜色
     * @return
     */
    public int getTitleBarColor(){
        return -1;
    }

    /**
     * 获取标题栏的字体颜色
     * @return
     */
    public int getTitleTextColor(){
        return R.color.color_font_black_white;
    }

    /**
     * 获取标题栏左侧的文字或图片（对应的点击事件clickTitleRight方法）
     *
     * @return null(默认)或""无文字;
     */
    public int getTitleLeftName() {
        return 0;
    }

    /**
     * 标题栏左侧文字点击事件的方法重写
     */
    public void onClickTitleLeft() {
    }

    /**
     * 1/3初始化布局
     */
    protected abstract void initView();

    /**
     * 2/3初始化数据
     */
    protected abstract void initData();

    /**
     * 3/3初始化监听
     */
    protected abstract void initListener();


    /**
     * （可调用方法）跳转界面
     *
     * @param clz
     */
    public void jumpToActivity(Class<? extends Activity> clz) {
        Intent intent = new Intent(ctx, clz);
        startActivity(intent);
    }
    /**
     * （可调用方法）跳转界面
     *
     * @param clz
     */
    public void jumpToActivity(Class<? extends Activity> clz, Bundle bundle) {
        Intent intent = new Intent(ctx, clz);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    //解决修改android手机设置中字体大小后系统布局混乱的方法
    //来源http://blog.csdn.net/biaobiao1217/article/details/51220075
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    @Override
    public void onBackPressed() {
        ActivityManager.getInstance().finishActivity();
    }

    @Override
    protected void onDestroy() {
        ToastUtils.cancel();
        super.onDestroy();
    }

    // dialog
    public CustomDialog getDialog() {
        if (dialog == null) {
            dialog = CustomDialog.instance(this);
            dialog.setCancelable(true);
        }
        return dialog;
    }

    public void hideDialog() {
        if (dialog != null)
            dialog.hide();
    }

    public void showDialog() {
        getDialog().show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }
}
